package hit.reports;

import hit.core.HomeInventoryTracker;
import hit.core.ItemIndex;
import hit.core.Item;
import hit.core.ProductGroup;
import hit.core.ProductIndex;
import hit.core.StorageUnit;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.Comparator;
import java.util.Iterator;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.HashMap;
import hit.core.Product;

/**
 * Builds a report for the list of Items removed from the system
 *
 */
public class RemovedItemsReport implements ReportDesigner, Visitor {
	
	Date limit;
	Map<Product, RemProductInfo> results;
	
	private class RemProductInfo
	{
		public Product product;
		public int removed;
		public int current;
		
		public RemProductInfo(Product product)
		{
			this.product = product;
			removed = 0;
			current = 0;
		}};

	
	static final Comparator<RemProductInfo> comp = new Comparator<RemProductInfo>()
	{
		public int compare(RemProductInfo i1, RemProductInfo i2)
		{				
			int result = i1.product.GetDescription().compareTo(i2.product.GetDescription());
			return result;
		}
	};
	
	public RemovedItemsReport(Date _limit){
		limit = _limit;
		results = new HashMap<Product, RemProductInfo>();
	}
	
	@Override
	public void BuildReport(ReportBuilder builder) {
		//Description, Size, Product Barcode, Removed, Current Supp	}
				
		SimpleDateFormat dateFormat = new SimpleDateFormat("M/d/yyyy HH:mm:ss");
		builder.setTitle("Items Removed Since " + dateFormat.format(limit));
		
		List<RemProductInfo> rem = new ArrayList<RemProductInfo>(results.values());
		Collections.sort(rem, comp);
		
		BuilderTable table = new BuilderTable();
		builder.AddObject(table);
		TableColumn desCol = new TableColumn("Description");
		table.AddColumn(desCol);
		
		TableColumn sizeCol = new TableColumn("Size");
		table.AddColumn(sizeCol);
		
		TableColumn barcodeCol = new TableColumn("Barcode");
		table.AddColumn(barcodeCol);
		
		TableColumn removedCol = new TableColumn("Removed");
		table.AddColumn(removedCol);
		
		TableColumn currentCol = new TableColumn("Current Supply");
		table.AddColumn(currentCol);
		
		for (RemProductInfo rpi : rem)
		{
			if (rpi.removed <= 0)
			{
				continue;
			}
			
			TableRow row = new TableRow();
			table.AddRow(row);
			row.AddItem(desCol, rpi.product.GetDescription());
			row.AddItem(sizeCol, rpi.product.GetSize().toString());
			row.AddItem(barcodeCol, rpi.product.GetBarcode().GetValue());
			row.AddItem(removedCol, Integer.toString(rpi.removed));
			row.AddItem(currentCol, Integer.toString(rpi.current));
		}
	}


	@Override
	public void VisitHITCore(HomeInventoryTracker core) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void VisitProductIndex(ProductIndex index) {
		// TODO Auto-generated method stub
	}

	@Override
	public void VisitItemIndex(ItemIndex index) {
		// TODO Auto-generated method stub
		
		Iterator<Item> iter = index.GetRemovedItemIterator();
		
		while (iter.hasNext())
		{
			Item item = iter.next();
			Date exit = item.GetExit();
			if (exit != null && exit.after(limit))
			{
				results.get(item.GetProduct()).removed++;
			}
		}
	}

	@Override
	public void VisitStorageUnit(StorageUnit unit) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void VisitProductGroup(ProductGroup group) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void VisitItem(Item item) {
		// TODO Auto-generated method stub
		
		if (item.GetExit() == null)
		{
			results.get(item.GetProduct()).current++;
		}
	}

	@Override
	public void VisitProduct(Product product) {
		// TODO Auto-generated method stub
		results.put(product, new RemProductInfo(product));
	}	
	
	@Override
	public void VisitProductGroupPost(ProductGroup pg) {
		// Do Nothing.
	}

}
